/**
 * 
 */
package info.niwota.ziplock;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

/**
 * @author qiangli
 * 
 */
public final class Notifications {
	private static final int SERVICE_ID = R.string.app_name;
	
	private static final Class<?> APP_CLASS = MainActivity.class;

	/**
	 * Add or update status bar icon
	 * @param ctx
	 */
	public static void addNotification(Context ctx) {
		try {
			int icon = (Preferences.isLoggedIn(ctx) ? R.drawable.icon25_secure : R.drawable.icon25);
			Notification note = new Notification(icon, null, 0);
			PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(
					ctx, APP_CLASS), 0);
			note.flags |= Notification.FLAG_NO_CLEAR;
			note.flags |= Notification.FLAG_ONGOING_EVENT;
			//
			CharSequence title = ctx.getText(R.string.app_name);
			CharSequence desc = ctx.getText(R.string.app_tagline);
			note.setLatestEventInfo(ctx, title, desc, pi);
			NotificationManager nm = (NotificationManager) ctx
					.getSystemService(Context.NOTIFICATION_SERVICE);
			nm.notify(SERVICE_ID, note);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Remove status bar icon
	 * @param ctx
	 */
	public static void removeNotification(Context ctx) {
		try {
			NotificationManager nm = (NotificationManager) ctx
					.getSystemService(Context.NOTIFICATION_SERVICE);
			//
			nm.cancelAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Notifications() {

	}

}
